package com.zj.leetcode.medium.greed;

import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @program: algorithm
 * @description: 根据身高重建队列
 * 406 queue-reconstruction-by-height
 * @author: Zhang Bo
 * @create: 2022-03-29 11:06:30
 **/
public class QueueReconstructionByHeight {
    public static void main(String[] args) {


//        输入：people = [[7,0],[4,4],[7,1],[5,0],[6,1],[5,2]]
//        输出：[[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]]  2, 5

        Solution solution = new QueueReconstructionByHeight().new Solution();
    }

    //leetcode submit region begin(Prohibit modification and deletion)
    class Solution {
        public int[][] reconstructQueue(int[][] people) {

            List<int[]> list =
                    Arrays.stream(people).sorted((a, b) -> {
                        if (a[0] == b[0]) {
                            return a[1] - b[1];
                        }
                        return b[0] - a[0];
                    }).collect(Collectors.toList());

            System.out.println(Arrays.deepToString(list.toArray()));
            LinkedList<int[]> que = new LinkedList<>();

            for (int[] p : list) {
                que.add(p[1],p);
            }

            return que.toArray(new int[people.length][]);
        }
    }
//leetcode submit region end(Prohibit modification and deletion)

}
